const {
  $Toast
} = require('../../utils/iview/base/index');
const Tools = require('../../utils/modules/Tools');
import NetWork from '../../utils/modules/NetWork';

const app = getApp();

var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
  data: {
    province: '',
    city: '广州市',
    type: 3,
    banner: null,
    student: null,
    shopList: null,
    //门店展示的数据
    shopname: [],
    indexs: 0,
    vehicle: [
    ],
    auth: false,
    isloade: {
      hidden: false
    },
    _baseUrl: app.globalData._baseUrl
  },

  onLoad(e) {
    var _that = this
    _that.setTitle('信息确认');



  },

  onShow: function() {
    let _that = this;

    //获取缓存中用户信息，然后去代码直接用缓存里面的数据
    var user = wx.getStorageSync('user');
    _that.setData({
      user: user,
    })

    _that.vehicleList();
  },


  addcar() {
    wx.navigateTo({
      // 跳出purchase ../
      // 进入personal 然后进入addVehicle中的addVehicle
      url: '../personal/addVehicle/addVehicle',

      // pages/personal/addVehicle / addVehicle
      // pages/personal/vehicleList / vehicleList
     
    })
  },


  //调取默认车辆的接口
  vehicleList() {
    //获取车辆列表
    var _that = this;
    var token = wx.getStorageSync('token');

    var net = new NetWork(_that.data._baseUrl);
    net._request({
      url: 'api/v1/member/mycar',
      data: {
        token: token
      },
      method: 'POST',
    }, '', res => {
      if (res.data.error_code == 0) {
        var min = res.data.data.data
        _that.setData({
          vehicle: min
        })
        //这边是获取数据（上框选中了那个id就是那个所以有了下面的if语句）vehicles数组里面有车牌号，车的类型等信息
        var vehicles = wx.getStorageSync('vehicles');

        for (var i = 0; i < min.length; i++) {
          if (vehicles && vehicles.id == min[i].id) {
            _that.setData({
              indexs: i
            })
          }
        }
        console.log(this.data.vehicle, "3")
      }
    }, res => {
      if (res.data.error_code == 400) {
        //token过期 重新执行该函数
        app.refreshtoken(token);
        setTimeout(() => {
          _that.users();
        }, 1500)

      }
    })
  },




  /* 拨打电话 */
  telPhone() {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: ''
    }, res => {

    });
  },

  setVehicle(e) {
    var { 
      index
    } = e.currentTarget.dataset;

    //设置缓存，点击选择的时候把vehicles数组里面的数据存储在里面，因为是点击选择然后上框就获取了车辆等信息所以应该在选择
    // 按钮在这边设置缓存
    //代码是bindtap="setVehicle"是选择按钮所以即写在js里边的setVehicle里面
    var vehicles = this.data.vehicle;
    wx.setStorageSync('vehicles', vehicles[index]);
    
    //这个就是选择完之后跳转到上页面去，就是退回返回页面
    setTimeout(() => {
      wx.navigateBack({
        changed: true
      });
    }, 1000)

    //这个就是选择完之后跳转到下页面去，一直选择就一直在页面循环，选择几个就返回几个才退的出去下单页面
    // wx.redirectTo({
    //   url: './purchase',
    // })
  },


  onReady: function(e) {

  },

  /* 修改头部标题 */
  setTitle(str) {
    wx.setNavigationBarTitle({
      title: str,
      success: function() {

      }
    })
  },
  onShareAppMessage(str) {
    return {
      title: str,
      path: this.route
    }
  },


  /* 隐藏分享功能 */
  hideShare() {
    wx.hideShareMenu()
  },
  /* 分享功能 */
  showShare() {
    wx.showShareMenu()
  },
  /* 下拉刷新触发功能 */
  onPullDownRefresh() {
    // 下拉刷新事件
    // wx.startPullDownRefresh
    // wx.stopPullDownRefresh
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)

  },

  /* 开启上拉到底部触发功能 */
  startDonwEven() {
    wx.startPullDownRefresh()
  },
  /* 停止上拉到底部触发功能 */
  stopDownEven() {
    wx.stopPullDownRefresh()
  },
  onReachBottom() {
    // 上拉触底事件
    //console.log('触发触底事件')
  },

  onPageScroll(res) {
    // 页面滚动事件
    //console.log(res.scrollTop)
  },

  userInfo(e) {
    let _userInfo = e.detail.userInfo;
    let net = new NetWork(app.globalData._baseUrl);

    if (_userInfo) {
      this.setData({
        auth: true
      })
    }

    wx.login({
      success(res) {
        //console.log('小程序登录信息', res)
        if (res.code) {
          net._request({
            url: 'login',
            data: {
              code: res.code
            },
          }, false, sres => {

          });
        }
      }
    })
  }


})